<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">

		
	<!-- 出院诊断章节 -->
	<xsl:template match="*" mode="HospitalDischargeDXSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HospitalDischargeDX]" mode="HospitalDischargeDXEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 出院诊断章节条目 -->
	<xsl:template match="*" mode="HospitalDischargeDXEntry">
		<xsl:choose>
		    <!-- 如果是《住院病案首页》 -->
			<xsl:when test="$hccDocType='C0032'">
				<xsl:apply-templates select="hl7:entry/hl7:organizer/hl7:component/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
					<xsl:with-param name="diagnosisType" select="''"/>
					<xsl:with-param name="sectionType" select="'DXD'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- 如果是其它文档 -->
			<xsl:otherwise>
				<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
					<xsl:with-param name="diagnosisType" select="''"/>
					<xsl:with-param name="sectionType" select="'DXD'"/>
			</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
		
	<!-- 入院诊断章节 -->
	<xsl:template match="*" mode="AdmissionDiagnosesSection">
	    <xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_HospitalAdmissionDX]" mode="AdmissionDXEntry"></xsl:apply-templates>
	</xsl:template>
	<!-- 入院诊断章节条目 -->
	<xsl:template match="*" mode="AdmissionDXEntry">
		<xsl:choose>
		    <!-- 如果是《24小时内入院死亡记录》、《24小时内入出院记录》 -->
			<xsl:when test="($hccDocType='C0035') or ($hccDocType='C0036')">
				<xsl:apply-templates select="hl7:entry/hl7:observation/hl7:entryRelationship/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
					<xsl:with-param name="diagnosisType" select="'入院诊断|入院诊断'"/>
					<xsl:with-param name="sectionType" select="'ADD'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- 如果是其它文档 -->
			<xsl:otherwise>
				<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
					<xsl:with-param name="diagnosisType" select="'入院诊断|入院诊断'"/>
					<xsl:with-param name="sectionType" select="'ADD'"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!--诊断记录章节-->
	<xsl:template match="*" mode="DiagnosesSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_Diagnosis]" mode="DiagnosesEntry"/>
	</xsl:template>
	<!-- 诊断记录章节条目 -->
	<xsl:template match="*" mode="DiagnosesEntry">
		<xsl:choose>
		<!-- 首次病程记录诊断记录章节 -->
		<xsl:when test="$hccDocType='C0037'">
			<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
				<xsl:with-param name="diagnosisType" select="''"/>
				<xsl:with-param name="sectionType" select="'D'"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="hl7:entry/hl7:organizer/hl7:component/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
				<xsl:with-param name="diagnosisType" select="''"/>
				<xsl:with-param name="sectionType" select="'D'"/>
			</xsl:apply-templates>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	
	<!--术后诊断章节-->
	<xsl:template match="*" mode="SurgicalOperationNotePostoperativeDxSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_SurgicalOperationNotePostoperativeDx]" mode="SONPosDxEntry"/>
	</xsl:template>
	<!-- 术后诊断章节条目 -->
	<xsl:template match="*" mode="SONPosDxEntry">
		<xsl:choose>
			<!-- 如果是《术后首次病程记录》 -->
			<xsl:when test="$hccDocType='C0048'">
				<xsl:apply-templates select="hl7:entry/hl7:observation/hl7:entryRelationship/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
					<xsl:with-param name="diagnosisType" select="'术后诊断|术后诊断'"/>
					<xsl:with-param name="sectionType" select="'SONPosDx'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- 如果是其它文档 -->
			<xsl:otherwise>
				<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
					<xsl:with-param name="diagnosisType" select="'术后诊断|术后诊断'"/>
					<xsl:with-param name="sectionType" select="'SONPosDx'"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!--术前诊断章节-->
	<xsl:template match="*" mode="SurgicalOperationNotePreoperativeDxSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_SurgicalOperationNotePreoperativeDx]" mode="SONPreDxEntry"/>
	</xsl:template>
	<!-- 术前诊断章节条目 -->
	<xsl:template match="*" mode="SONPreDxEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
			<xsl:with-param name="diagnosisType" select="'术前诊断|术前诊断'"/>
			<xsl:with-param name="sectionType" select="'SONPreDx'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!--死亡原因章节-->
	<xsl:template match="*" mode="DeathReasonSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_DeathReason]" mode="DeathReasoEntry"/>
	</xsl:template>
	<!-- 死亡原因章节条目 -->
	<xsl:template match="*" mode="DeathReasoEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation/hl7:entryRelationship/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
			<xsl:with-param name="diagnosisType" select="'死亡原因|死亡原因'"/>
			<xsl:with-param name="sectionType" select="'DR'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 卫生事件章节 -->
	<xsl:template match="*" mode="HealthcareEventDXSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_HealthcareEvent]" mode="HealthcareEventDXEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 卫生事件章节条目 -->
	<xsl:template match="*" mode="HealthcareEventDXEntry">
		<!--<xsl:variable name="tmpDiagnosisType"><xsl:value-of select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]/hl7:code/hl7:qualifier/hl7:name/@displayName"/></xsl:variable>-->
		<xsl:apply-templates select="hl7:entry/hl7:observation[(hl7:code/@code='DE05.01.024.00') or (hl7:code/@code='DE05.10.130.00')]" mode="HCCDiagnosis">
			<!--<xsl:with-param name="diagnosisType" select="concat($tmpDiagnosisType,'|',$tmpDiagnosisType)"/>-->
			<xsl:with-param name="diagnosisType" select="''"/>
			<xsl:with-param name="sectionType" select="'HED'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- 卫生事件章节(根本死因代码) -->
	<xsl:template match="*" mode="HealthcareEventDDXSection">
		<xsl:apply-templates select="$sectionRootPath[hl7:code/@displayName=$hcc_HealthcareEvent]" mode="HealthcareEventDDXEntry">
		</xsl:apply-templates>
	</xsl:template>
	<!-- 卫生事件章节(根本死因代码)条目 -->
	<xsl:template match="*" mode="HealthcareEventDDXEntry">
		<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:code/@code='DE05.01.021.00']" mode="HCCDiagnosis">
			<xsl:with-param name="diagnosisType" select="'根本死因|根本死因'"/>
			<xsl:with-param name="sectionType" select="'HED'"/>
		</xsl:apply-templates>
	</xsl:template>
	
	
	<!-- 主要健康问题章节 (处理C0013，C0024，C0034) -->
	<xsl:template match="*" mode="ProblemListDXSection">
		<xsl:choose>
		    <!-- C0013，C0024，C0034 处理 -->
			<xsl:when test="$hccDocType='C0013' or $hccDocType='C0024' or $hccDocType='C0034' ">
				<xsl:apply-templates select="$sectionRootPath[hl7:code/@code=$hcc_ProblemList]" mode="ProblemListDXEntry">
			</xsl:apply-templates>			
		</xsl:when>
		</xsl:choose>
	</xsl:template>
	<!-- 主要健康问题章节条目 -->
	<xsl:template match="*" mode="ProblemListDXEntry">
		<xsl:choose>
			<!-- for C0034 -->
			<xsl:when test="$hccDocType='C0034'">
				<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:entryRelationship/hl7:observation/hl7:code/@code='DE05.01.024.00' or hl7:entryRelationship/hl7:observation/hl7:code/@code='DE05.10.130.00']" mode="HCCDiagnosis">
					<xsl:with-param name="sectionType" select="'PLD'"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- for C0013, C0024 -->
			<xsl:otherwise>
				<xsl:apply-templates select="hl7:entry/hl7:observation[hl7:code/@code='DE05.01.024.00']" mode="HCCDiagnosis">
					<xsl:with-param name="sectionType" select="'PLD'"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>

	
	<!--通用诊断条目输出模板-->
	<xsl:template match="*" mode="HCCDiagnosis">
		<xsl:param name = "diagnosisType" />
		<xsl:param name = "sectionType" />
		<xsl:variable name="HCCHEDdiagnosisType" select="hl7:code/hl7:qualifier/hl7:name/@displayName"/>
		<xsl:variable name="HCCdiagnosisType" select="hl7:code/@displayName"/>
		
		<xsl:choose>
			<xsl:when test="$sectionType='PLD'">
				<xsl:choose>
					<!-- for C0034 -->
					<xsl:when test="$hccDocType='C0034'">
					<Diagnosis>
						<Diagnosis>
							<Code><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE05.01.024.00' or hl7:code/@code='DE05.10.130.00']/hl7:value/@code"/></Code>
							<Description><xsl:value-of select="hl7:entryRelationship/hl7:observation[hl7:code/@code='DE05.01.024.00' or hl7:code/@code='DE05.10.130.00']/hl7:value/@displayName"/></Description>
						</Diagnosis>
						<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
						<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
						<DiagnosisType>			
							<Code><xsl:value-of select="hl7:code/@displayName"/></Code>
							<Description><xsl:value-of select="hl7:code/@displayName"/></Description>
						</DiagnosisType>
						<OnsetTime><xsl:value-of select="isc:evaluate('xmltimestamp', hl7:effectiveTime/@value)"/></OnsetTime>
					</Diagnosis>
					</xsl:when>
					<!-- for C0013, C0024 -->
					<xsl:otherwise>
					<Diagnosis>
						<Diagnosis>
							<Code><xsl:value-of select="hl7:value/@code"/></Code>
							<Description><xsl:value-of select="hl7:value/@displayName"/></Description>
						</Diagnosis>
						<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
						<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
						<DiagnosisType>			
							<Code><xsl:value-of select="'主要健康问题'"/></Code>
							<Description><xsl:value-of select="'主要健康问题'"/></Description>
						</DiagnosisType>
					</Diagnosis>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<Diagnosis>
					<Diagnosis>
						<Code><xsl:value-of select="hl7:value/@code"/></Code>
						<Description><xsl:value-of select="hl7:value/@displayName"/></Description>
					</Diagnosis>
					<EncounterNumber><xsl:value-of select="$encompassingEncounterID"/></EncounterNumber>
					<ExternalId><xsl:value-of select="concat($hccDocNo,'-',$sectionType,'-',position())"/></ExternalId>
					<xsl:choose>
						<xsl:when test="string-length($diagnosisType)">
							<DiagnosisType>			
								<Code><xsl:value-of select="substring-before($diagnosisType, '|')"/></Code>
								<Description><xsl:value-of select="substring-after($diagnosisType, '|')"/></Description>
							</DiagnosisType>
							<DiagnosisPriority><xsl:value-of select="$HCCdiagnosisType"/></DiagnosisPriority>
						</xsl:when>
						<xsl:otherwise>
						    <xsl:choose>
								<xsl:when test="$sectionType='HED'">
									<DiagnosisType>			
										<Code><xsl:value-of select="$HCCHEDdiagnosisType"/></Code>
										<Description><xsl:value-of select="$HCCHEDdiagnosisType"/></Description>
									</DiagnosisType>
								</xsl:when>
								<xsl:otherwise>
									<DiagnosisType>			
										<Code><xsl:value-of select="$HCCdiagnosisType"/></Code>
										<Description><xsl:value-of select="$HCCdiagnosisType"/></Description>
									</DiagnosisType>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</Diagnosis>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
